package com.css.fxfzaqbz.modules.data.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.css.fxfzaqbz.constants.YNEnum;
import com.css.fxfzaqbz.modules.data.entity.HistoricalSeismicData;
import com.css.fxfzaqbz.modules.data.entity.HistoricalSeismicDataDTO;
import com.css.fxfzaqbz.modules.data.queryParams.HistoricalSeismicDataQueryParams;
import com.css.fxfzaqbz.modules.data.repository.HistoricalSeismicDataRepository;
import com.css.fxfzaqbz.modules.data.service.HistoricalSeismicDataService;
import com.css.fxfzaqbz.util.FileUtil2;
import com.css.fxfzaqbz.util.PlatformDateUtils;
import com.css.fxfzaqbz.util.PlatformPageUtils;
import com.css.fxfzaqbz.util.UUIDGenerator;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class HistoricalSeismicDataServiceImpl implements HistoricalSeismicDataService {
    @Resource
    HistoricalSeismicDataRepository historicalSeismicDataRepository;

//    @Override
//    @Transactional(rollbackFor = Exception.class)
//    public HistoricalSeismicData updateHistoricalSeismicData(HistoricalSeismicData historicalSeismicData) {
//
//        historicalSeismicData.setHsdUpdatetime(PlatformDateUtils.getCurrentTimestamp());
//
//        return activeFaultDataRepository.updateActiveFaultData(activeFaultData);
//    }

    @Override
    @Transactional
    public HistoricalSeismicData saveHistoricalSeismicData(HistoricalSeismicData historicalSeismicData) {
        historicalSeismicData.setHsdId(UUIDGenerator.getUUID());
        historicalSeismicData.setHsdStatus(YNEnum.Y.toString());
        historicalSeismicData.setHsdCreatetime(PlatformDateUtils.getCurrentTimestamp());
        return historicalSeismicDataRepository.saveHistoricalSeismicData(historicalSeismicData);
    }

    @Override
    public JSONObject getAllHistoricalSeismicData(HistoricalSeismicDataQueryParams queryParams) {
        return PlatformPageUtils.formatPageData(historicalSeismicDataRepository.getAllHistoricalSeismicDataParams(queryParams));
    }

    @Override
    public HistoricalSeismicData getHistoricalSeismicDataById(String id) {
        HistoricalSeismicData historicalSeismicData = historicalSeismicDataRepository.getHistoricalSeismicDataById(id);
        return historicalSeismicData;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteHistoricalSeismicDatas(List<String> ids) {
        Timestamp timestamp = PlatformDateUtils.getCurrentTimestamp();
        historicalSeismicDataRepository.deleteHistoricalSeismicDatasById(ids,timestamp);
    }

    @Override
    public List<HistoricalSeismicData> getAll() {

        return historicalSeismicDataRepository.getAll();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void importExcel(MultipartFile file) throws IOException, ParseException {
        List<HistoricalSeismicDataDTO> historicalSeismicDataDTO = FileUtil2.importExcel(file, HistoricalSeismicDataDTO.class);
        for(int i = 0 ; i<historicalSeismicDataDTO.size();i++){
            historicalSeismicDataDTO.get(i).setHsdId(UUIDGenerator.getUUID());
            historicalSeismicDataDTO.get(i).setHsdStatus(YNEnum.Y.toString());
            historicalSeismicDataDTO.get(i).setHsdCreatetime(PlatformDateUtils.getCurrentTimestamp());

            String s = historicalSeismicDataDTO.get(i).getHsdSeismictime();
            System.out.println(s);
            Date date = new Date();
            date  =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(s);
            historicalSeismicDataDTO.get(i).setTypeTime(date);
        }
        historicalSeismicDataRepository.importExcel(historicalSeismicDataDTO);
    }

}
